package com.chinacoal.hr.levelimpl.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinacoal.hr.levelapi.entity.AttendanceAppeal;
import com.chinacoal.hr.levelimpl.mapper.AttendanceAppealMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-11-17 12:13
 * @description:
 **/
@Component
public class AttendanceAppealDao {

    @Resource
    private AttendanceAppealMapper attendanceAppealMapper;

    /**
     * 查询考勤申诉列表
     *
     * @param condition :
     * @return : com.baomidou.mybatisplus.core.metadata.IPage<com.chinacoal.hr.levelimpl.entity.AttendanceAppeal>
     * @author : YunTao.Li
     * @date : 2019/11/20 2019/11/20
     */
    public IPage<AttendanceAppeal> getAttendanceAppealList(Map<String, Object> condition) {
        Long currentPage = (Long) condition.get("page");
        Long currentPageSize = (Long) condition.get("limit");
        IPage<AttendanceAppeal> page = new Page(currentPage, currentPageSize);

        String orgId = condition.get("orgId") != null ? condition.get("orgId").toString() : "";
        String orgCode = condition.get("orgCode") != null ? condition.get("orgCode").toString() : "";

        String yrmonth = condition.get("yrmonth") != null ? condition.get("yrmonth").toString() : "";
        String empNo = condition.get("empNo") != null ? condition.get("empNo").toString() : "";
        String empCode = condition.get("empCode") != null ? condition.get("empCode").toString() : "";
        String explainCode = condition.get("explainCode") != null ? condition.get("explainCode").toString() : "";

        QueryWrapper queryWrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(orgId)) {
            queryWrapper.eq("OU_ID", orgId);
        }
        if (StringUtils.isNotBlank(yrmonth)) {
            queryWrapper.eq("PERIOD_CODE", yrmonth);
        }
        if (StringUtils.isNotBlank(empNo)) {
            queryWrapper.eq("EMP_ID", empNo);
        }
        if (StringUtils.isNotBlank(explainCode)) {
            queryWrapper.eq("EXPLAIN_CODE", explainCode);
        }
        if (StringUtils.isNotBlank(orgCode)) {
            queryWrapper.eq("ORG_CODE", orgCode);
        }
        if (StringUtils.isNotBlank(empCode)) {
            queryWrapper.eq("EMP_CODE", empCode);
        }

        queryWrapper.orderByDesc("LAST_UPDATE_DATE");


        IPage<AttendanceAppeal> attendanceAppeal = attendanceAppealMapper.selectPage(page, queryWrapper);

        return attendanceAppeal;
    }

    /**
     * 根据查询条件获得考勤申诉单信息
     * 如果有多条记录，取第一条
     *
     * @param condition :
     * @return : void
     * @author : YunTao.Li
     * @date : 2019/11/27 2019/11/27
     */
    public AttendanceAppeal getAttendanceAppeal(Map condition) {
        String explainCode = condition.get("explainCode") != null ? condition.get("explainCode").toString() : "";
        String explainId = condition.get("explainId") != null ? condition.get("explainId").toString() : "";
        String processInstID = condition.get("processInstID") != null ? condition.get("processInstID").toString() : "";


        QueryWrapper wrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(explainCode)) {
            wrapper.eq("EXPLAIN_CODE", explainCode);
        }
        if (StringUtils.isNotBlank(explainId)) {
            wrapper.eq("EXPLAIN_ID", explainId);
        }
        if (StringUtils.isNotBlank(processInstID)) {
            wrapper.eq("INS_CODE", processInstID);
        }
        wrapper.last("limit 1");

        AttendanceAppeal attendanceAppeal = this.attendanceAppealMapper.selectOne(wrapper);
        return attendanceAppeal;
    }


    /**
     * 新增考勤申诉信息
     *
     * @param record :
     * @return : void
     * @author : YunTao.Li
     * @date : 2019/11/20 2019/11/20
     */
    public void insertAttendanceAppeal(AttendanceAppeal record) {
        // 新增默认的编码单
        attendanceAppealMapper.insert(record);
    }

    /**
     * 修改考勤申诉信息
     *
     * @param record :
     * @return : void
     * @author : YunTao.Li
     * @date : 2019/11/20 2019/11/20
     */
    public void updateAttendanceAppeal(AttendanceAppeal record) {
        attendanceAppealMapper.updateById(record);
    }

    /**
     * 删除考勤申诉信息
     *
     * @param explainId :
     * @return : void
     * @author : YunTao.Li
     * @date : 2019/11/20 2019/11/20
     */
    public void deleteAttendanceAppealById(String explainId) {
        attendanceAppealMapper.deleteById(explainId);
    }
}
